package com.lipuwater.agent.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import com.lipuwater.agent.R;
import com.ismartlib.utils.DisplayUtil;

/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2016/12/15.
 * Email:924686754@qq.com
 */
public class HintEditText extends EditText implements TextWatcher, View.OnFocusChangeListener {
    private Context mContext;
    /**
     * 字符总长度
     */
    private int mStrCount = 20;
    /**
     * 当前字符长度
     */
    private int mCurrentStrCount = 0;

    private Paint mPaint;
    /**
     * 删除按钮的引用
     */
    private Drawable mClearDrawable;
    private boolean hasFoucs;

    public HintEditText(Context context) {
        this(context, null);
    }

    public HintEditText(Context context, AttributeSet attrs) {
        //这里构造方法很重要，不加这个很多属性不能再XML里面定义
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public HintEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        InputFilter[] filters = {new InputFilter.LengthFilter(mStrCount)};
        setFilters(filters);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(DisplayUtil.dip2px(context, 0.1f));
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(0xffb3b3b3);
        mPaint.setTextSize(DisplayUtil.dip2px(context, 12));
        init();
    }

    private void init() {
        mClearDrawable = getCompoundDrawables()[2];
        if (mClearDrawable == null) {
            mClearDrawable = ContextCompat.getDrawable(mContext, R.drawable.ll_btn_delete_normal);
        }
        mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
        //默认设置隐藏图标
        setClearIconVisible(false);
        //设置焦点改变的监听
        setOnFocusChangeListener(this);
    }

    protected void setClearIconVisible(boolean visible) {
        Drawable right = visible ? mClearDrawable : null;
        setCompoundDrawables(getCompoundDrawables()[0],
                getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFoucs = hasFocus;
        if (hasFocus) {
            setClearIconVisible(getText().length() > 0);
        } else {
            setClearIconVisible(false);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
                        && (event.getX() < ((getWidth() - getPaddingRight())));
                if (touchable) {
                    this.setText("");
                }
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        String drawText = String.valueOf(mStrCount - mCurrentStrCount);
        canvas.drawText(drawText,
                getWidth() - getTextWidth(drawText, mPaint) - DisplayUtil.dip2px(mContext, 5),
                getHeight() - (int) getTextHeight(mPaint),
                mPaint);
    }


    /**
     * 当输入框里面内容发生变化的时候回调
     */
    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after) {
        if (hasFoucs) {
            setClearIconVisible(s.length() > 0);
        }
        if (mPaint == null) {
            return;
        }
        mCurrentStrCount = s.length();
        invalidate();//调起重绘
        setPadding(DisplayUtil.dip2px(mContext, 9),
                DisplayUtil.dip2px(mContext, 10),
                DisplayUtil.dip2px(mContext, 10)
                        + getTextWidth(String.valueOf(mStrCount - mCurrentStrCount), mPaint)
                , DisplayUtil.dip2px(mContext, 10));
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }


    public void setStrCount(int strCount) {
        this.mStrCount = strCount;
        InputFilter[] filters = {new InputFilter.LengthFilter(mStrCount)};
        setFilters(filters);
    }


    private int getTextWidth(String strings, Paint p) {
        float maxWidthF = 0.0f;
        float width = p.measureText(strings);
        maxWidthF = Math.max(width, maxWidthF);
        int maxWidth = (int) (maxWidthF + 0.5);
        if (maxWidth < 0) {
            maxWidth = 0;
        }
        return maxWidth;
    }

    public double getTextHeight(Paint mPaint) {
        Paint.FontMetrics fm = mPaint.getFontMetrics();
        return Math.ceil(fm.descent - fm.ascent);
    }


}
